/*
 * Copyright (c) Beicaizs.com Inc.
 * All Rights Reserved.
 */
package com.zlb.envmodify.transform.transformer;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.Map;

public class EarlyClassCacheTransformer implements ClassFileTransformer {

    private static EarlyClassCacheTransformer sington;

    Map<String,byte[]> cache = new HashMap<String,byte[]>();

    public static EarlyClassCacheTransformer getSington() {
        if(sington==null){
            synchronized (EarlyClassCacheTransformer.class){
                if(sington==null){
                    sington = new EarlyClassCacheTransformer();
                }
            }
        }
        return sington;
    }

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        cache.put(className,classfileBuffer);
        return classfileBuffer;
    }

    public byte[] getClassByte(String className){
        return cache.get(className);
    }

    public Map<String,byte[]> getClassByte(){
        return cache;
    }

    public boolean contains(String className) {
        return cache.containsKey(className);
    }

    public void put(String className, byte[] classByte) {
        cache.put(className,classByte);
    }
}
